package com.qst.binzhou2022.services;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qst.binzhou2022.beans.UserInfo;
import com.qst.binzhou2022.daos.UserInfoDao;
import com.qst.binzhou2022.mapper.UserInfoMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class UserInfoService {

	

	public boolean getUserInfo(UserInfo userInfo) throws SQLException, IOException{
		UserInfoDao userInfoDao = new UserInfoDao();
		
		boolean flag = false;
		
		List<UserInfo> userInfoList  = userInfoDao.getUserInfo(userInfo);
		
		if(userInfoList!=null && userInfoList.size()==1){
			
			flag = true;
		}
		
		return flag;
	}


	public boolean addUserInfo(UserInfo userInfo) throws SQLException, IOException{
		UserInfoDao userInfoDao = new UserInfoDao();

		boolean flag = false;

		int addResult  = userInfoDao.addUserInfo(userInfo);

		if(addResult==1){

			flag = true;
		}

		return flag;
	}


	/*
Mybatis接口代理
 */
	public boolean getUserInfoInter(UserInfo userInfo) throws SQLException, IOException{
		//获取
		String resource = "sqlMapConfig.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();

		//通过getMapper方法获取到一个userInfoMapper接口的实现类的对象
		UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
		UserInfo userInfo1 = new UserInfo();
			//数据筛选条件
		userInfo1.setUserId(1);
		//更新的数据
		userInfo1.setUserName("aaaaaa");
		userInfo1.setUserSex("9");
		int result  = userInfoMapper.updateUserInfo(userInfo1);
		sqlSession.commit();

		return true;
	}

}
